<?php
/**
 * @version SVN: $Id$
 * @package    fmcSeminaire
 * @subpackage Models
 * @author     EasyJoomla {@link http://www.easy-joomla.org Easy-Joomla.org}
 * @author     Ludovic Launer {@link }
 * @author     Created on 09-Nov-2010
 * @license    GNU/GPL
 */

//-- No direct access
defined('_JEXEC') or die('=;)');

jimport('joomla.application.component.model');

/**
 * fmcSeminaire Model
 *
 * @package    fmcSeminaire
 * @subpackage Models
 */
class fmcSeminaireModelfmcSeminaire extends JModel
{
    /**
     * Gets the data.
     *
     * @return string The data to be displayed to the user
     */
    function getData()
    {
    	// --- Retrieve params ---
    	// I know this should not be there ! value should be passed as a param
    	$campagneId = fmcSeminaireController::getCampagneId();
    	
        $db =& JFactory::getDBO();

        $query = '	SELECT * 
        			FROM #__fmcseminaire s JOIN #__fmcseminaire_campagnes c ON (s.campagneId=c.campagneId) 
        			WHERE s.campagneId=?';
        $query = str_replace('?', $campagneId, $query);
        $db->setQuery($query);
        $data = $db->loadObjectList();

        return $data;
    }
    
    function getSeminaireDates()
    {
    	// --- Retrieve params ---
    	// I know this should not be there ! value should be passed as a param
    	$campagneId = fmcSeminaireController::getCampagneId();
    	
    	$db =& JFactory::getDBO();

        $query = '	SELECT 	dateId,
           					DATE_FORMAT(dateDebut, \'%d/%m/%Y\') as dateDebut,
        					DATE_FORMAT(dateDebut + INTERVAL nbJours-1 DAY, \'%d/%m/%Y\') as dateFin,
        					nbJours 
        			FROM #__fmcseminaire_dates 
        			WHERE campagneId=?
        			ORDER BY #__fmcseminaire_dates.dateDebut ASC
        			';
        
        $query = preg_replace('/\?/', $campagneId, 		$query, 1);
        
        $db->setQuery($query);
        $data = $db->loadObjectList();

        return $data;
    }
// _____________________________________________________________________________________________
// submitWishes
// Checks data OK + Submit votes into DB   
// _____________________________________________________________________________________________ 
  function submitWishes($vote, $seminaires, $dates )
    {
    	$db =& JFactory::getDBO();
    	
		// --- Vote ---
  		$dbres = $db->insertObject( '#__fmcseminaire_votes', $vote, 'voteId' ) ;
    	if (!$dbres) if (!$dbres) throw new Exception($db->ErrorMsg());
    		
  		$_voteId=$vote->voteId;
  		
    	// --- Seminaires ---
    	foreach ($seminaires as $sem):
    		$sem->voteId = $_voteId;
    		$dbres = $db->insertObject( '#__fmcseminaire_vote_has_seminaire', $sem, 'votehasseminaireId' ) ;
		    if (!$dbres) throw new Exception($db->ErrorMsg());
    	endforeach;
    	
    	// --- Dates ---
    	foreach ($dates as $dat):
    		$dat->voteId = $_voteId;
    		$dbres = $db->insertObject( '#__fmcseminaire_vote_has_date', $dat, 'votehasdateId' ) ;
    		
		    if (!$dbres) if (!$dbres) throw new Exception($db->ErrorMsg());
    	endforeach;
    }
// _____________________________________________________________________________________________
// isVoteAlreadySubmited
// Checks data OK + Submit votes into DB   
// _____________________________________________________________________________________________     
  function isVoteAlreadySubmited($email, $campagneId, $campagneName) {
	$db =& JFactory::getDBO();
	
	$query = '	SELECT 	*
        		FROM #__fmcseminaire_votes v JOIN #__fmcseminaire_campagnes c ON (v.campagneId=c.campagneId) 
        		WHERE 
        			email LIKE \'?\'
        			AND v.campagneId=?
        		;';
	
	$email		= trim($email);
	$query = preg_replace('/\?/', $email, 		$query, 1);
	$query = preg_replace('/\?/', $campagneId, 	$query, 1);
	
	$db->setQuery($query);
	$dbres = $db->loadObject();

	if ($dbres) {
		$campagneName = $dbres->campagneName;
		return true;
	}
	
  	return false;
	}   

}//class
